#ifndef CONFIG_H
#define CONFIG_H

#include <QObject>
#include <QJsonObject>
#include <QQmlEngine>

#include "../global/xnamespace.h"

X_BEGIN_NAMESPACE

// 应用程序配置，除给C++使用，还可以给QML使用，通过属性方式。
class Config : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString domain READ domain WRITE setDomain NOTIFY domainChanged)
    Q_PROPERTY(int port READ port WRITE setPort NOTIFY portChanged)
    Q_PROPERTY(int sslPort READ sslPort WRITE setSslPort NOTIFY sslPortChanged)
    Q_PROPERTY(bool ignoreSslError READ ignoreSslError WRITE setIgnoreSslError)
	Q_PROPERTY(QString defaultAvatar READ defaultAvatar NOTIFY defaultAvatarChanged)
    Q_PROPERTY(QString cloudDesktopHome READ cloudDesktopHome WRITE setCloudDesktopHome NOTIFY cloudDesktopHomeChanged)
    Q_PROPERTY(bool rememberPassword READ rememberPassword WRITE setRememberPassword NOTIFY rememberPasswordChanged)
    Q_PROPERTY(bool autoLogin READ autoLogin WRITE setAutoLogin NOTIFY autoLoginChanged)

signals:
    void domainChanged(QString domain);
    void portChanged(int port);
    void sslPortChanged(int sslPort);
    void cloudDesktopHomeChanged(QString cloudDesktopHome);
    void rememberPasswordChanged(bool rememberPassword);
    void autoLoginChanged(bool autoLogin);
	void defaultAvatarChanged(QString avatar);

public:
    static Config &instance();

    static const char *qmlName();
    static QObject *qmlInstance(QQmlEngine* engine, QJSEngine* jsEngine);

public: // 属性
    QString domain(bool appendPrefix = true) const;
    int port() const;
    int sslPort() const;
    bool ignoreSslError() const;
    QString defaultAvatar() const;
    QString cloudDesktopHome() const;
    bool rememberPassword() const;
    bool autoLogin() const;
    QString storagePath(const QString& bareJid, const QString& subPath = "") const;
    QString resourcesPath() const;

public slots:
    Config &setDomain(QString domain);
    Config &setPort(int port);
    Config &setSslPort(int sslPort);
    Config &setIgnoreSslError(bool ignoreSslError);
    Config &setCloudDesktopHome(QString cloudDesktopHome);
    Config &setRememberPassword(bool rememberPassword);
    Config &setAutoLogin(bool autoLogin);

private:
    Config();
    void init();
    static Config *createInstance();

private:
    static const QString JSON_FILE;

    QJsonObject m_json;

public:
    static const char VCards[];
    static const char Images[];
};

X_END_NAMESPACE

#endif // CONFIG_H
